package com.qxzn.module.mine.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.FamilyMemberBean;
import com.qxzn.module.mine.R;
import com.qxzn.module.mine.R2;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FamilyMemberAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Activity activity;
    private List<FamilyMemberBean.MemberBean> dataList;
    private FamilyMemberBean.MemberBean item;
    private String attribute;
    private boolean isEdit;
    private View.OnClickListener itemClick;
    private View.OnClickListener viewClick;

    public FamilyMemberAdapter(Activity activity, List dataList) {
        this.activity = activity;
        this.dataList = dataList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 3) {
            return new ViewHolder3(LayoutInflater.from(activity).inflate(R.layout.item_not_family_member, parent, false));
        } else {
            return new ViewHolder1(LayoutInflater.from(activity).inflate(R.layout.item_family_member, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder1, int position) {
        item = dataList.get(position);
        attribute = "";
        if (holder1 instanceof ViewHolder1) {
            ViewHolder1 holder = (ViewHolder1) holder1;
            Glide.with(activity).load(HttpRequest.BASE_URL + item.getHeadportrait()).apply(RequestOptions.circleCropTransform().placeholder(R.mipmap.icon_xiaopai).error(R.mipmap.icon_xiaopai)).into(holder.ivHead);
            holder.tvName.setText(item.getNiname());
            if (!TextUtils.isEmpty(item.getTitle())) {
                holder.tvTitle.setText(item.getTitle());
                holder.tvTitle.setVisibility(View.VISIBLE);
            } else {
                holder.tvTitle.setVisibility(View.GONE);
            }
            if ("1".equals(item.getAdminstate())) {
                attribute = "管理员  ";
                holder.ivCheck.setVisibility(View.GONE);
            } else {
                if (isEdit) {
                    holder.ivCheck.setVisibility(View.VISIBLE);
                    if (item.isCheck()) {
                        holder.ivCheck.setImageResource(R.mipmap.icon_member_check);
                    } else {
                        holder.ivCheck.setImageResource(R.mipmap.icon_member_uncheck);
                    }
                } else {
                    holder.ivCheck.setVisibility(View.GONE);
                }
            }
            if (position == 0) {
                attribute += "自己";
            }
            holder.tvAttribute.setText(attribute);
            holder.itemView.setTag(item);
            holder.itemView.setOnClickListener(itemClick);
        } else {
            ViewHolder3 holder = (ViewHolder3) holder1;
            Glide.with(activity).load(HttpRequest.BASE_URL + item.getHeadportrait()).apply(RequestOptions.circleCropTransform().placeholder(R.mipmap.icon_xiaopai).error(R.mipmap.icon_xiaopai)).into(holder.ivHead);
            holder.tvName.setText(item.getNiname());
            if (!TextUtils.isEmpty(item.getTitle())) {
                holder.tvTitle.setText(item.getTitle());
                holder.tvTitle.setVisibility(View.VISIBLE);
            } else {
                holder.tvTitle.setVisibility(View.GONE);
            }
            if (isEdit) {
                holder.ivCheck.setVisibility(View.VISIBLE);
                if (item.isCheck()) {
                    holder.ivCheck.setImageResource(R.mipmap.icon_member_check);
                } else {
                    holder.ivCheck.setImageResource(R.mipmap.icon_member_uncheck);
                }
            } else {
                holder.ivCheck.setVisibility(View.GONE);
            }
            if ("-1".equals(item.getRoles())) {
                attribute = "请求加入家庭圈";
            } else if ("-2".equals(item.getRoles())) {
                attribute = "等待对方同意";
            } else if ("-3".equals(item.getRoles())) {
                attribute = "您已拒绝对方加入";
            } else if ("-4".equals(item.getRoles())) {
                attribute = "对方已拒绝";
            }
            holder.tvAttribute.setText(attribute);
            holder.itemView.setTag(item);
            holder.itemView.setOnClickListener(itemClick);
            holder.tvView.setTag(item);
            holder.tvView.setOnClickListener(viewClick);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return dataList.get(position).getType();
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    public void setItemClick(View.OnClickListener itemClick) {
        this.itemClick = itemClick;
    }

    public void setViewClick(View.OnClickListener viewClick) {
        this.viewClick = viewClick;
    }

    static class ViewHolder1 extends RecyclerView.ViewHolder {
        @BindView(R2.id.tv_title)
        TextView tvTitle;
        @BindView(R2.id.iv_check)
        ImageView ivCheck;
        @BindView(R2.id.iv_head)
        ImageView ivHead;
        @BindView(R2.id.iv_right)
        ImageView ivRight;
        @BindView(R2.id.tv_name)
        TextView tvName;
        @BindView(R2.id.tv_attribute)
        TextView tvAttribute;

        ViewHolder1(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder3 extends RecyclerView.ViewHolder {
        @BindView(R2.id.tv_title)
        TextView tvTitle;
        @BindView(R2.id.iv_check)
        ImageView ivCheck;
        @BindView(R2.id.iv_head)
        ImageView ivHead;
        @BindView(R2.id.tv_view)
        TextView tvView;
        @BindView(R2.id.tv_name)
        TextView tvName;
        @BindView(R2.id.tv_attribute)
        TextView tvAttribute;

        ViewHolder3(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
